<HTML> 
<HEAD> 
	<TITLE>3rd Party ROBOTC Drivers</TITLE> 
<link href="tabs.css" rel="stylesheet" type="text/css"/> 
<link href="doxygen.css" rel="stylesheet" type="text/css"/> 
</HEAD> 
 
<BODY bgcolor="#ffffff" link="#000000" vlink="#000000"> 
<table width="100%" bgcolor="navy" cellspacing=0 cellpadding=1 border=0> 
<tr><td><table width="100%" bgcolor="#EEEEDD" cellspacing=0 cellpadding=3 border=0> 
<tr> 
    <td width="33%" align="left"> <img src="images/LOGO_NXT.gif" width=266 height=44 border="0" alt="Mindstorms"></td> 
    <td><b><font size="+3" color="navy">3rd Party ROBOTC Drivers</font></b></td> 
    <td align="right"> <img src="images/logo.png" width=44 height=44 border="0" alt="RobotC"></td>     
</tr> 
  <tr bgcolor="#cccc99" align="right"> 
          <td colspan=3> <font face="arial" size="-1"> [<a href=main.html>Home</a>] [<a target=_top href="https://sourceforge.net/projects/rdpartyrobotcdr/">Download</a>] [<a target=_top href="http://apps.sourceforge.net/mantisbt/rdpartyrobotcdr/my_view_page.php">Submit a bug/suggestion</a>]  [<a target=_top href="http://www.robotc.net/forums/">ROBOTC Forums</a>] [<a target=_top href="http://botbench.com">Blog</a>] [<a target=_top href="http://sourceforge.net/donate/index.php?group_id=257238">Support this project</a>]</font> </td> 
</tr> 
</table></td></tr> 
</table> 
<!-- Generated by Doxygen 1.7.2 -->
<div class="header">
  <div class="headertitle">
<h1>hitechnic-irlink-test1.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma config(Sensor, S1,     HTIRL,               sensorI2CCustom)</span>
<span class="preprocessor"></span><span class="comment">//*!!Code automatically generated by &#39;ROBOTC&#39; configuration wizard               !!*//</span>

<span class="comment">/*</span>
<span class="comment"> * $Id: hitechnic-irlink-test1.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * hitechnic-irlink.h provides an API for the HiTechnic IR Link Sensor.  This program</span>
<span class="comment"> * demonstrates how to use the Power Functions API to control up to 8 motors.</span>
<span class="comment"> *</span>
<span class="comment"> * Changelog:</span>
<span class="comment"> * - 0.1: Initial release</span>
<span class="comment"> * - 0.2: More comments</span>
<span class="comment"> * - 0.3: Complete rewrite, can demo all three supported modes.</span>
<span class="comment"> *</span>
<span class="comment"> * Credits:</span>
<span class="comment"> * - Big thanks to HiTechnic for providing me with the hardware necessary to write and test this.</span>
<span class="comment"> *</span>
<span class="comment"> * License: You may use this code as you wish, provided you give credit where it&#39;s due.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. </span>
<span class="comment"></span>
<span class="comment"> * Xander Soldaat (xander_at_botbench.com)</span>
<span class="comment"> * 16 September 2010</span>
<span class="comment"> * version 0.3</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;<a class="code" href="hitechnic-irlink_8h.html" title="HiTechnic IR Link Sensor driver.">drivers/hitechnic-irlink.h</a>&quot;</span>

<span class="comment">/*</span>
<span class="comment">  =============================================================================</span>
<span class="comment">  Function prototypes</span>
<span class="comment"></span>
<span class="comment"> */</span>
<span class="keywordtype">void</span> doPFcomboPwmModeTest(byte speed);
<span class="keywordtype">void</span> doPFcomboDirectModeTest(byte speed);
<span class="keywordtype">void</span> doPFsinglePinOutputModeTest(byte speed);
<span class="keywordtype">void</span> stopMotors();

<span class="comment">/*</span>
<span class="comment">  =============================================================================</span>
<span class="comment">  main task with some testing code</span>
<span class="comment"></span>
<span class="comment"> */</span>
task main {

  <span class="comment">// Variables</span>
        byte mode = 0;  <span class="comment">// current mode</span>
        byte speed = 0; <span class="comment">// current speed</span>

  nxtDisplayCenteredTextLine(0, <span class="stringliteral">&quot;HiTechnic&quot;</span>);
  nxtDisplayCenteredBigTextLine(1, <span class="stringliteral">&quot;IRLink&quot;</span>);
  nxtDisplayCenteredTextLine(3, <span class="stringliteral">&quot;Test 1&quot;</span>);
  nxtDisplayCenteredTextLine(5, <span class="stringliteral">&quot;Connect sensor&quot;</span>);
  nxtDisplayCenteredTextLine(6, <span class="stringliteral">&quot;to S1&quot;</span>);
  wait1Msec(2000);
  eraseDisplay();

  nxtDisplayCenteredTextLine(0, <span class="stringliteral">&quot;IRLink Test 1&quot;</span>);
  nxtDisplayTextLine(3, <span class="stringliteral">&quot;Press [ENTER]&quot;</span>);
  nxtDisplayTextLine(4, <span class="stringliteral">&quot;to switch modes&quot;</span>);
  nxtDisplayTextLine(6, <span class="stringliteral">&quot;Press &lt; and &gt;&quot;</span>);
  nxtDisplayTextLine(7, <span class="stringliteral">&quot;to change speed&quot;</span>);

  <span class="keywordflow">while</span> (<span class="keyword">true</span>) {
    <span class="comment">// Decrease the speed when Left Button is pressed</span>
    <span class="keywordflow">if</span> (nNxtButtonPressed == kLeftButton)
      speed--;

    <span class="comment">// Increase speed when the Right Button is pressed</span>
                <span class="keywordflow">else</span> <span class="keywordflow">if</span> (nNxtButtonPressed == kRightButton)
                  speed++;

                <span class="comment">// Switch mode when the Enter Button is pressed</span>
                <span class="keywordflow">else</span> <span class="keywordflow">if</span> (nNxtButtonPressed == kEnterButton) {
                  speed = 0;
                  stopMotors();           <span class="comment">// Stop the motors</span>
                  mode++;
                  <span class="keywordflow">if</span> (mode &gt; 2) mode = 0; <span class="comment">// Make sure mode does not overflow</span>
                }
    <span class="comment">// Debounce the button</span>
                <span class="keywordflow">while</span> (nNxtButtonPressed != kNoButton) EndTimeSlice();

                <span class="comment">// Make sure speed is between 0 and 15</span>
                <span class="keywordflow">if</span> (speed &lt; 0) speed = 0;
                <span class="keywordflow">else</span> <span class="keywordflow">if</span> (speed &gt; 15) speed = 15;

                <span class="keywordflow">switch</span> (mode) {
      <span class="keywordflow">case</span> 0: doPFcomboPwmModeTest(speed); <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> 1: doPFcomboDirectModeTest(speed); <span class="keywordflow">break</span>;
      <span class="keywordflow">case</span> 2: doPFsinglePinOutputModeTest(speed); <span class="keywordflow">break</span>;
    }
    <span class="keywordflow">while</span> (nNxtButtonPressed == kNoButton) EndTimeSlice();
  }
}

<span class="comment">// Run the motors using the Combo PWM Mode</span>
<span class="keywordtype">void</span> doPFcomboPwmModeTest(byte speed) {
  nxtDisplayCenteredTextLine(1, <span class="stringliteral">&quot;comboPWM %d&quot;</span>, speed);
  <a name="a0"></a><a class="code" href="group__htirl.html#ga9f9d921939a7bcf5ec940d5ad0c2dab2">PFcomboPwmMode</a>(HTIRL, 0, (<a class="code" href="group__htirl.html#ga9c3c0063406d1dabd1b18f2b27e83487">ePWMMotorCommand</a>)speed, (<a class="code" href="group__htirl.html#ga9c3c0063406d1dabd1b18f2b27e83487">ePWMMotorCommand</a>)(15 - speed));
}

<span class="comment">// Run the motors using the Combo Direct Mode</span>
<span class="keywordtype">void</span> doPFcomboDirectModeTest(byte speed) {
  <span class="keywordflow">if</span> (speed &gt; 3) speed = 3;
  nxtDisplayCenteredTextLine(1, <span class="stringliteral">&quot;comboDirect %d&quot;</span>, speed);
  <a name="a1"></a><a class="code" href="group__htirl.html#ga3f38d6d4421a9e49bd4ea79ebe1012d6">PFcomboDirectMode</a>(HTIRL, 0, (<a class="code" href="group__htirl.html#gac9c310f7de4ef728365b829d296d9b34">eCDMMotorCommand</a>)speed, (<a class="code" href="group__htirl.html#gac9c310f7de4ef728365b829d296d9b34">eCDMMotorCommand</a>)(3 - speed));
}

<span class="comment">// Run the motors using the Single Pin Output Mode</span>
<span class="keywordtype">void</span> doPFsinglePinOutputModeTest(byte speed) {
  nxtDisplayCenteredTextLine(1, <span class="stringliteral">&quot;PFMotor %d&quot;</span>, speed);
        <a name="a2"></a><a class="code" href="group__htirl.html#ga7dd0cfb4544284046a7528f1c9fb18c5">PFMotor</a>(<a name="a3"></a><a class="code" href="group__htirl.html#gga39dc14fd7972d5ad5fddd8cd1a62fd5ba7494aff8c386091c4ff989e7a369b201">pfmotor_S1_C1_A</a>, (<a class="code" href="group__htirl.html#ga9c3c0063406d1dabd1b18f2b27e83487">ePWMMotorCommand</a>)speed);
        <a class="code" href="group__htirl.html#ga7dd0cfb4544284046a7528f1c9fb18c5">PFMotor</a>(<a name="a4"></a><a class="code" href="group__htirl.html#gga39dc14fd7972d5ad5fddd8cd1a62fd5bad0dcdc02cb2762dc4d1317325cdbd17f">pfmotor_S1_C1_B</a>, (<a class="code" href="group__htirl.html#ga9c3c0063406d1dabd1b18f2b27e83487">ePWMMotorCommand</a>)(15 - speed));
}

<span class="comment">// Stop the motors</span>
<span class="keywordtype">void</span> stopMotors() {
  <a class="code" href="group__htirl.html#ga3f38d6d4421a9e49bd4ea79ebe1012d6">PFcomboDirectMode</a>(HTIRL, 0, <a name="a5"></a><a class="code" href="group__htirl.html#ggac9c310f7de4ef728365b829d296d9b34a824870df549686104759f89a4398dacd">CDM_MOTOR_BRAKE</a>, <a class="code" href="group__htirl.html#ggac9c310f7de4ef728365b829d296d9b34a824870df549686104759f89a4398dacd">CDM_MOTOR_BRAKE</a>);
}

<span class="comment">/*</span>
<span class="comment"> * $Id: hitechnic-irlink-test1.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Mar 13 2013 19:56:42 for ROBOTC Drivers by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
